package com.cryptoneedle.center.doris.service.doris;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.cryptoneedle.center.common.constants.CommonConstant;
import com.cryptoneedle.center.common.entity.doris.DorisCatalog;
import com.cryptoneedle.center.common.entity.doris.DorisColumn;
import com.cryptoneedle.center.common.entity.doris.DorisDatabase;
import com.cryptoneedle.center.common.entity.doris.DorisTable;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>description: Doris 服务 </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@DS("pg")
@Slf4j
public class DorisSyncService {

    @Resource
    private DorisCatalogService dorisCatalogService;

    @Resource
    private DorisDatabaseService dorisDatabaseService;

    @Resource
    private DorisTableService dorisTableService;

    @Resource
    private DorisColumnService dorisColumnService;

    public void syncDoris() {
        // 同步 Catalog
        List<DorisCatalog> dorisCatalogList = dorisCatalogService.syncDorisCatalog();

        // 同步 Database，仅同步 internal
        String catalogName = CommonConstant.DORIS_CATALOG;
        List<DorisDatabase> dorisDatabaseList = dorisDatabaseService.syncDorisDatabase(catalogName);
        for (DorisDatabase dorisDatabase : dorisDatabaseList) {
            String databaseName = dorisDatabase.getDatabaseName();

            // 同步 Table
            List<DorisTable> dorisTableList = dorisTableService.syncDorisTable(catalogName, databaseName);

            // 同步 Column
            List<DorisColumn> dorisColumnList = dorisColumnService.syncDorisColumn(catalogName, databaseName);
        }
    }

    public List<DorisColumn> syncDorisColumn(String catalogName, String databaseName, String tableName) {
        // 同步 Column
        return dorisColumnService.syncDorisColumn(catalogName, databaseName, tableName);
    }
}